package com.zx.idc.backend.gui.base.cus;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zx.idc.backend.gui.base.cus.service.BaseCustomerReportService;
import com.zx.idc.backend.gui.common.BaseApi;
import com.zx.idc.backend.gui.common.Result;
import com.zx.idc.backend.gui.common.base.BaseExcelApi;
import com.zx.idc.ds.base.cus.entity.BaseCustomer;
import com.zx.idc.ds.base.cus.service.IBaseCustomerService;
import com.zx.idc.ds.base.cus.vo.BaseCustomerVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 客户（用户）信息管理 前端控制器
 * </p>
 *
 * @author rxliuli
 * @since 2019-01-31
 */
@RestController
@RequestMapping("/api/baseCustomer")
public class BaseCustomerController extends BaseApi implements BaseExcelApi<IBaseCustomerService, BaseCustomerVo> {
    @Autowired
    private IBaseCustomerService service;
    @Autowired
    private BaseCustomerReportService baseCustomerReportService;

    @GetMapping("/get/{id}")
    @RequiresPermissions("basic-data:base-customer")
    public Result<BaseCustomerVo> get(@PathVariable Long id) {
        return success(service.get(id));
    }

    @GetMapping("/get-by-hold-id/{customerId}/{holdId}")
    @RequiresPermissions("basic-data:base-customer")
    public Result<BaseCustomerVo> getByHoldId(@PathVariable Long customerId, @PathVariable Long holdId) {
        return success(service.getByHoldId(customerId, holdId));
    }

    @GetMapping("/get-by-service-id/{customerId}/{serviceId}")
    @RequiresPermissions("basic-data:base-customer")
    public Result<BaseCustomerVo> getByServiceId(@PathVariable Long customerId, @PathVariable Long serviceId) {
        return success(service.getByServiceId(customerId, serviceId));
    }

    @GetMapping("/get-for-all-base-houses-hold-info/{id}")
    @RequiresPermissions("basic-data:base-customer")
    public Result<BaseCustomerVo> getForAllBaseHousesHoldInfo(@PathVariable Long id) {
        return success(service.getForAllBaseHousesHoldInfo(id));
    }

    @GetMapping("/select-list")
    @RequiresPermissions("basic-data:base-customer")
    public Result<List<BaseCustomer>> selectList() {
        return success(service.selectList(new EntityWrapper<BaseCustomer>().setSqlSelect("id", "unitName")));
    }

    @GetMapping("/query-page")
    @RequiresPermissions("basic-data:base-customer")
    public Result<Page<BaseCustomerVo>> page(Page<BaseCustomerVo> page, BaseCustomerVo baseCustomerVo) {
        return success(service.pageByParam(page, baseCustomerVo));
    }

    @PostMapping("/add")
    @RequiresPermissions("basic-data:base-customer:edit")
    public Result<Boolean> add(@RequestBody BaseCustomerVo baseCustomerVo) {
        System.out.println(1/0);
        return success(service.add(baseCustomerVo));
    }

    /**
     * 更新客户及其相关信息
     */
    @PostMapping("/update")
    @RequiresPermissions("basic-data:base-customer:edit")
    public Result<Boolean> update(@RequestBody BaseCustomerVo baseCustomerVo) {
        String msg;
        try {
            return success(service.update(baseCustomerVo));
        } catch (Exception e) {
            msg = e.getMessage();
            log.error("修改客户信息异常", e);
        }
        return error(msg);
    }

    @PostMapping("/add-base-houses-hold-info-by-customer-id")
    @RequiresPermissions("basic-data:base-customer:edit")
    public Result<Boolean> addBaseHousesHoldInfoByCustomerId(@RequestBody BaseCustomerVo baseCustomerVo) {
        String msg;
        try {
            return success(service.addBaseHousesHoldInfoByCustomerId(baseCustomerVo));
        } catch (Exception e) {
            msg = e.getMessage();
            log.error("新增应用服务或占用机房信息异常", e);
        }
        return error(msg);
    }

    @PostMapping("/delete/batch")
    @RequiresPermissions("basic-data:base-customer:edit")
    public Result<Boolean> deleteBatchByIds(@RequestBody List<Long> idList) {
        return success(service.deleteBatchByIds(idList));
    }

    @PostMapping("/insert-report/{id}")
    @RequiresPermissions("basic-data:base-customer:edit")
    public Result<Boolean> insertReport(@PathVariable Long id) {
        String msg;
        try {
            return success(baseCustomerReportService.insertReport(id));
        } catch (Exception e) {
            msg = e.getMessage();
            log.error("客户数据 新增上报异常", e);
        }
        return error(msg);
    }

    @PostMapping("/update-report/{id}")
    @RequiresPermissions("basic-data:base-customer:edit")
    public Result<Boolean> updateReport(@PathVariable Long id) {
        String msg;
        try {
            return success(baseCustomerReportService.updateReport(id));
        } catch (Exception e) {
            msg = e.getMessage();
            log.error("客户数据 修改上报异常", e);
        }
        return error(msg);
    }
}
